<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo.jpg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="login.html">登陆</a>
    <!-- <a href="#">注销</a> -->
</div>
<!-- 版心 -->
<div class="login-container">
    <!-- 中间的注册框 -->
    <div class="login-dialog" style="height: 460px;">
        <h3>注册</h3>
        <div class="row">
            <span>登录名</span>
            <input type="text" id="username">
        </div>
        <!--<div class="row">
            <span>昵称</span>
            <input type="text" id="nickname">
        </div>-->
        <div class="row">
            <span>密码</span>
            <input type="password" id="password">
        </div>
        <div class="row">
            <span>确认密码</span>
            <input type="password" id="password2">
        </div>
        <div class="row" style="margin-bottom: 20px;">
            <span>验证码</span>
            <input id="code_input" style="width: 66px;">&nbsp;&nbsp;
            <img onclick="refreshCode()" id="codeimg" src="img/check_code.png"
                 style="height: 50px;width: 128px;">
        </div>
        <div class="row">
            <button id="submit" onclick="doReg()">提交</button>
        </div>
    </div>
</div>

<script>
    // 验证码key
    var codeKey = "";

    // 获取并显示验证码
    function refreshCode() {
        jQuery.ajax({
            url:'/getcaptcha',
            type:'GET',
            data:{},
            success:function (body) {
                if(body.code==200&&body.data!=null&&body.data!=""){
                    // 获取验证码成功
                    codeKey=body.data.codeKey;
                    jQuery('#codeimg').attr('src',body.data.codeUrl);
                }
            }
        })
    }

    refreshCode();
    function doReg() {
        // 进行非空校验
        var username = jQuery('#username')
        var password = jQuery('#password')
        var password2 = jQuery('#password2')
        var checkCode = jQuery('#code_input')
        if (username.val().trim() == '') {
            alert('请输入用户名！')
            username.focus()
            return false
        }
        if (password.val().trim() == '') {
            alert('请输入密码！')
            password.focus()
            return false
        }
        if (password2.val().trim() == '') {
            alert('请输入确认密码！')
            password2.focus()
            return false
        }
        if (checkCode.val().trim() == '') {
            alert('请输入验证码！')
            checkCode.focus()
            return false
        }

        // 密码与确认密码是否一致
        if (password.val() != password2.val()) {
            alert('两次密码输入不一致，请重新输入密码！')
            password.focus()
            return false
        }
        // 发送Ajax请求
        jQuery.ajax({
            url: '/user/reg',
            type: 'post',
            data: {
                "codeKey":codeKey,
                "username": username.val(),
                "password": password.val(),
                "checkCode": checkCode.val()
            },
            // 接受返回结果
            success: function (body) {
                if (body.code == 200 && body.data == 1) {
                    alert("恭喜,注册成功!");
                    if (confirm("是否要去登录页面 ?")) {
                        location.href = "login.html";
                    }
                } else if (body.data == -1) {
                    alert("抱歉, 注册失败, 请重新注册!")
                } else {
                    alert("该用户名已被使用, 请重新输入!");
                }
            }
        })
    }
</script>
</body>

</html>